<?php
class omedlyexport_ctl_ome_delivery extends desktop_controller{
    function index(){
    	set_time_limit ( 0 );
        $title = '';
        if(isset($_POST['delivery_bn']) && $_POST['delivery_bn']){
            $deliveryObj = &app::get('ome')->model('delivery');
            $rows = $deliveryObj->getParentIdBybn($_POST['delivery_bn']);
            if($rows){
                foreach($rows as $val){
                    $deliveryId[] = $val['parent_id'];
                }
                $filter['extend_delivery_id'] = $deliveryId;
            }
        }
        $filter['type'] = 'normal';
        $status_cfg = &app::get('ome')->getConf('ome.delivery.status.cfg');
        //分析status的filter条件
        $tmp_filter = $this->analyseStatus($_POST['status']);
        $filter = array_merge($filter,$tmp_filter);

        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_ids = $oBranch->getBranchByUser(true);
           if ($branch_ids){
                $filter['branch_id'] = $branch_ids;
           }else{
                $filter['branch_id'] = 'false';
           }
        }
        $attach = '&status='.$_POST['status'].'&logi_id='.$_POST['logi_id'];
        $params = array(
                        'title'=>$filter['_title_'],
                        'actions' => array(
                                'stock' => array(
                                    'label' => '打印备货单',
                                    'submit' => 'index.php?app=ome&ctl=admin_receipts_print&act=toPrintStock'.$attach,
                                    'target' => "_blank",
                                ),
                                'delie' => array(
                                    'label' => '打印发货单',
                                    'submit' => 'index.php?app=ome&ctl=admin_receipts_print&act=toPrintMerge'.$attach,
                                    'target' => '_blank',
                                ),
                                'expre' => array(
                                    'label' => '打印快递单',
                                    'submit' => 'index.php?app=ome&ctl=admin_receipts_print&act=toPrintShip'.$attach,
                                    'target' => '_blank',//"dialog::{width:800,height:600,title:'设置标签'}",//
                                ),
                        ),
                        'base_filter' => $filter,
                        'use_buildin_new_dialog' => false,
                        'use_buildin_set_tag'=>false,
                        'use_buildin_recycle'=>false,
                        'use_buildin_export'=>false,
                        'use_buildin_import'=>false,
                        'use_buildin_filter'=>true,
                        'max_actions'=>8,
                        'use_view_tab'=>true,
                        //从载方法 以解决 发货中未录入快递单号不能过滤的bug
                        'object_method'=>array('count'=>'count_logi_no','getlist'=>'getlist_logi_no'),
                    );
                    //选择显示打印的按钮
        if($status_cfg != ''){
		        if($status_cfg['set']['stock'] == 0){
		            unset($params['actions']['stock']);
		        }
		        if($status_cfg['set']['delie'] == 0){
		            unset($params['actions']['delie']);
		        }
        }
        //暂停列表不显示按钮
        if($_POST['status'] == 6){
        	unset($params['actions']['stock']);
        	unset($params['actions']['delie']);
        	unset($params['actions']['style']);
        	unset($params['actions']['expre']);
        }
        unset($_POST['status']);

        $this->finder('omedlyexport_mdl_ome_delivery', $params);
    }

    function analyseStatus($status, $type='normal'){
    	$status_cfg = &app::get('ome')->getConf('ome.delivery.status.cfg');
        if ($type == 'normal'){
        switch ($status){
            case '':
                $title = '全部';
                $filter = array();
                $filter['pause'] = "FALSE";
                break;
            case 0:
                $title = '待打印';
                if($status_cfg != ''){
                    if(($status_cfg['set']['stock'] == 1 && $status_cfg['set']['delie'] == 1) ){
                        $filter['todo'] = "1";
                        $filter['pause'] = "FALSE";
                        $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
                    }
                    if($status_cfg['set']['stock'] == 1 && $status_cfg['set']['delie'] == 0){
                        $filter['todo'] = "2";
                        $filter['pause'] = "FALSE";
                        $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
                    }
                    if($status_cfg['set']['stock'] == 0 && $status_cfg['set']['delie'] == 1){
                        $filter['todo'] = "3";
                        $filter['pause'] = "FALSE";
                        $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
                    }
                    if($status_cfg['set']['stock'] == 0 && $status_cfg['set']['delie'] == 0 ){
                         $filter['todo'] = "4";
                         $filter['pause'] = "FALSE";
                         $filter['verify'] = 'FALSE';
                         $filter['process'] = "FALSE";
                    }
                }else{
                    $filter['todo'] = "1";
                    $filter['pause'] = "FALSE";
                    $filter['verify'] = 'FALSE';
                    $filter['process'] = "FALSE";
                }
                break;
            case 1:
                $title = '已打印';
                if($status_cfg != ''){
	                if(($status_cfg['set']['stock'] == 1 && $status_cfg['set']['delie'] == 1) ){
	                    $filter['stock_status'] = "TRUE";
	                    $filter['deliv_status'] = "TRUE";
	                    $filter['expre_status'] = "TRUE";
	                    $filter['pause'] = "FALSE";
	                    $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
	                }
	                if($status_cfg['set']['stock'] == 1 && $status_cfg['set']['delie'] == 0){
	                    $filter['expre_status'] = "TRUE";
	                    $filter['stock_status'] = "TRUE";
	                    $filter['pause'] = "FALSE";
	                    $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
	                }
			        if($status_cfg['set']['stock'] == 0 && $status_cfg['set']['delie'] == 1){
			            $filter['expre_status'] = "TRUE";
			            $filter['deliv_status'] = "TRUE";
			            $filter['pause'] = "FALSE";
			            $filter['verify'] = 'FALSE';
                        $filter['process'] = "FALSE";
			        }
			        if($status_cfg['set']['stock'] == 0 && $status_cfg['set']['delie'] == 0 ){
			             $filter['expre_status'] = "TRUE";
			             $filter['pause'] = "FALSE";
			             $filter['verify'] = 'FALSE';
                         $filter['process'] = "FALSE";
			        }
                }else{
                	$filter['stock_status'] = "TRUE";
                    $filter['deliv_status'] = "TRUE";
                    $filter['expre_status'] = "TRUE";
                    $filter['pause'] = "FALSE";
                    $filter['verify'] = 'FALSE';
                    $filter['process'] = "FALSE";
                }
                break;
            case 2:
                $title = '未录入物流单号';
                $filter['logi_no'] = 'NULL';
                $filter['pause'] = "FALSE";
                break;
            case 3:
                $title = '已校验';
                $filter['verify'] = 'TRUE';
                $filter['process'] = "FALSE";
                $filter['pause'] = "FALSE";
                break;
            case 4:
                $title = '未发货';
                $filter['process'] = "FALSE";
                $filter['pause'] = "FALSE";
                break;
            case 5:
                $title = '已发货';
                $filter['process'] = "TRUE";
                $filter['pause'] = "FALSE";
                break;
            case 6:
                $title = '暂停列表';
                $filter['pause'] = "TRUE";
                break;
        }
        }elseif ($type == 'refunded'){
            switch ($status){
                case '':
                    $title = '未发货';
                    $filter['process'] = "FALSE";
                    $filter['pause'] = "FALSE";
                    break;
                case 1:
                    $title = '未发货';
                    $filter['process'] = "FALSE";
                    $filter['pause'] = "FALSE";
                    break;
                case 2:
                    $title = '已发货';
                    $filter['process'] = "TRUE";
                    $filter['pause'] = "FALSE";
                    break;
            }
        }
        //默认条件
        $filter['parent_id'] = 0;
        $filter['disabled'] = 'false';
        $filter['status'] = array('ready','progress','succ');
        $filter['_title_'] = $title;

        return $filter;
    }
}